import VueCookies from 'vue-cookies'
import queryString from 'query-string'
import store from 'store'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const version = require('../../version.json').version
/*新增色号兼容*/
const color = {
  White: ['White', 'white', 'light'],
  Black: ['Black', 'dark', 'black'],
  Blue: ['Blue'],
  OldBlack: ['night'],
  OldWhite: ['plain'],
}
export const getTheme = (type = 'Black') => {
  let result = 'Black'
  Object.keys(color).map(item => {
    if (color[item].indexOf(type) > -1) {
      result = item
    }
  })
  return result
}
/**
 * 修改主题
 */
const updateTheme = (type = 'black') => {
  type = getTheme(type)
  document.getElementById('themeStyle').href = `./style/${type}.base.css?date=${version}`
  VueCookies.set('themeType', type, -1)
  store.dispatch('changeThemeType', type)
}
/**
 * 修改字体
 */
const updateFontSize = data => {
  const fontArray = [12, 13, 14, 15, 16, 18, 20, 24]
  fontArray.map(item => {
    document.body.style.setProperty(`--size_${item}`, `${item + data - 12}px`)
  })
  VueCookies.set('zjs_fontSize', data, -1)
  store.dispatch('updateFontSize', data)
}

/**
 * 初始化主题
 */
export const initTheme = () => {
  const hrefArray = window.location.href.split('?')
  let themeType = ''
  if (hrefArray[1]) {
    themeType = queryString.parse(hrefArray[1]).themeType
  }
  let defaultTheme = 'Black'
  if (process.env.VUE_APP_MODULE == 'report_pro' || process.env.VUE_APP_MODULE == 'report_test') {
    defaultTheme = 'White'
  }

  themeType =
    themeType || (VueCookies.isKey('themeType') ? VueCookies.get('themeType') : defaultTheme)
  updateTheme(themeType)
}
/**
 * 初始化字体
 */
export const initSize = size => {
  const fontSize = size || (VueCookies.isKey('zjs_fontSize') ? +VueCookies.get('zjs_fontSize') : 12)
  updateFontSize(fontSize)
}

/**
 * 初始化保存用户id
 */
export const initUserId = () => {
  const hrefArray = window.location.href.split('?')
  if (hrefArray[1]) {
    const userId = queryString.parse(hrefArray[1]).userId
    if (userId) VueCookies.set('userId', userId, -1)
  }
}

/**
 * 界面初始化
 */
export const initPage = () => {
  // 初始化主题
  initTheme()
  // 保存用户id
  initUserId()
  // 初始化字体
  // alert(process.env.VUE_APP_ENV);
  if (process.env.VUE_APP_MODULE == 'report_pro' || process.env.VUE_APP_MODULE == 'report_test') {
    initSize(14)
  } else {
    initSize()
  }
}

/**
 * 接收native端事件调用
 */
export const nativeActions = (type, data) => {
  console.log(type, data)
  switch (type) {
    case 'themeType':
      return updateTheme(data)
    case 'fontSize':
      return updateFontSize(data)
  }
}
